import Qt 4.7
import "config.js" as Options

ListModel {
    id: jsonModel
    property string url: "http://127.0.0.1"
    property string path
    property variant json
    property variant tmp44: setPath(path)

    signal loaded
    signal error

    function reload() {
        _load();
    }

    function find(object) {
        var found_index = null
        for(var i = 0; i < count; i += 1) {
            var item = get(i)
            var is_item = true
            for(var prop in object) {
                if(!object.hasOwnProperty(prop)) {
                    continue;
                }
                if(object[prop] !== item[prop]) {
                    is_item = false
                    break
                }
            }

            if(is_item) {
                found_index = i
                break
            }
        }
        return found_index
    }

    function findAll(object) {
        var result = []

        for(var i = 0; i < count; i += 1) {
            var item = get(i)
            var is_item = true
            for(var prop in object) {
                if(!object.hasOwnProperty(prop)) {
                    continue;
                }
                if(object[prop] !== item[prop]) {
                    is_item = false
                    break
                }
            }

            if(is_item) {
                result.push(i)
            }
        }
        return result
    }

    function firstOrCreate(object) {
        var found_index = null
        var found_item = null
        found_index = find(object)

        if(found_index === null) {
            found_index = count
            found_item = object
            insert(found_index, found_item)
        }
        else {
            found_item = get(found_item)
        }
        found_item.index = found_index
        return found_item
    }

    function setPath(path) {
        if(path === "") {
            return
        }

        _load();
    }

    function _load() {
        url = Options.server || url;
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if(xhr.readyState === XMLHttpRequest.DONE) {
                if(xhr.status === 200) {
                    console.log(xhr.responseText);
                    console.log(xhr.getAllResponseHeaders())
                    json = eval(xhr.responseText);
                    loaded();
                }
                else {
                    console.log("GET ERROR", url+path, xhr.status, xhr.responseText)
                    error()
                }
            }
        }
        console.log("GET", url+path)
        xhr.open("GET", url + path);
        xhr.send();
    }

    function _post(path, cookie) {
        url = Options.server || url;
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if(xhr.readyState === XMLHttpRequest.DONE) {
                if(xhr.status === 200) {
                    console.log(xhr.responseText);
                    json = []
                    loaded();
                }
                else {
                    console.log("POST ERROR", url+path, xhr.status, xhr.responseText)
                    error()
                }
            }
        }
        console.log("POST", url+path, json)
        xhr.open("POST", url + path);
        if(cookie !== "") {
            xhr.setRequestHeader('Cookie', cookie)
        }
        xhr.send(json);
    }

    function toArray() {
        var result = []
        for(var i = 0; i < count; i += 1) {
            result[i] = get(i)
        }
        return result
    }
}
